package org.hncors.gnssparse;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

// TODO: Auto-generated Javadoc


public class SelectTemplateWithAcceptPointRefreshDialog extends DialogFragment {
	
	private int mUniqueIdentifier = -1;
	private ArrayList<String> mItemInfoLinkedList;
	private onCustomDialogWithAcceptPointRefreshListener mOnListener;
	private boolean mIsUIModeInput = false;
	private String mLastEditInputDataString;
	private Animation animation;
	
	private ImageView mRefreshView;
	private ImageView mPenView;
	private TextView mTitleView;
	
	private ListView mListView;
	private EditText mEditText;
	private SelectorItemAdapter mAdapter;
	
	private int mSelectIndex = -1;

	public static SelectTemplateWithAcceptPointRefreshDialog newInstance(
			String title,
			ArrayList<String> itemsList,
			int defaultIndex, 
			int uniqueIdentifier
			) {
		SelectTemplateWithAcceptPointRefreshDialog pcid = new SelectTemplateWithAcceptPointRefreshDialog();
		Bundle args = new Bundle();
		args.putString("SelectTemplateTitle", title);
		args.putSerializable("SelectTemplateListData", (ArrayList<String>)itemsList.clone());
		args.putInt("SelectTemplateDefault", defaultIndex);
		args.putInt("SelectTemplateIdentifier", uniqueIdentifier);
		pcid.setArguments(args);
		return pcid;
	}
	

	public interface onCustomDialogWithAcceptPointRefreshListener {

		public abstract void onCustomDialogWithAcceptPointSelectListener(int uniqueIdentifier, int selectIndex, ArrayList<String> data);
		

		public abstract void onCustomDialogWithAcceptPointRefreshPleaseListener();
		

		public abstract void onCustomDIalogWithAcceptPointEditInputListener(int uniqueIdentifier, String data);
	}
	
	@Override
	public void onCreate(Bundle saveBundle) {
		super.onCreate(saveBundle);
		if (saveBundle != null) {
			mIsUIModeInput = saveBundle.getBoolean("CurrentUIMode");
			mLastEditInputDataString = saveBundle.getString("PreEditInputData");
		}
	}
	
	@Override
	public void onSaveInstanceState(Bundle outBundle) {
		outBundle.putBoolean("CurrentUIMode", mIsUIModeInput);
		outBundle.putString("PreEditInputData", mEditText.getText().toString());
	}

	private void UpdateUIMode(boolean isInput) {
		if (mListView == null || mEditText == null) return;
		
		if (isInput == true) {
			mListView.setVisibility(View.GONE);
			mEditText.setVisibility(View.VISIBLE);
	        if (getDialog() != null) ((CustomAlertDialog)getDialog()).GetAlertControl().GetButtonPanel().getChildAt(0).setVisibility(View.VISIBLE);
		} else {
			mListView.setVisibility(View.VISIBLE);
			mEditText.setVisibility(View.GONE);
			if (getDialog() != null) ((CustomAlertDialog)getDialog()).GetAlertControl().GetButtonPanel().getChildAt(0).setVisibility(View.GONE);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Dialog onCreateDialog(Bundle saveInstanceState) {
		animation = AnimationUtils.loadAnimation(getActivity(), R.anim.loading_animation);
		Log.e("onCreateDialog", "SelectTemplateWithAcceptPointRefreshDialog");
		String title = getArguments().getString("SelectTemplateTitle");
		ArrayList<String> data = (ArrayList<String>)getArguments().getSerializable("SelectTemplateListData");
		if (data != null) mItemInfoLinkedList = (ArrayList<String>) data.clone();
		mSelectIndex = getArguments().getInt("SelectTemplateDefault");
		mUniqueIdentifier = getArguments().getInt("SelectTemplateIdentifier");
		
		if (mItemInfoLinkedList == null) mItemInfoLinkedList = new ArrayList<String>();
		mItemInfoLinkedList.add(0, getActivity().getString(R.string.AcceptPointrefreshPlease));

		// dialog builder
		CustomAlertDialog.Builder builder = new CustomAlertDialog
						.Builder(getActivity())
						.setNegativeButton(getString(R.string.global_cancel), null)
						.setPositiveButton(getString(R.string.global_sure), new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface arg0, int arg1) {
								if (mOnListener != null)
									mOnListener.onCustomDIalogWithAcceptPointEditInputListener(mUniqueIdentifier, mEditText.getText().toString());
							}
		});

		// add custom title view
		View titleView = getActivity().getLayoutInflater().inflate(R.layout.custom_alert_title_with_refresh, null);
		if (titleView != null) {
			mPenView = (ImageView) titleView.findViewById(R.id.pen);
			if (mPenView != null)
				mPenView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						mIsUIModeInput = !mIsUIModeInput;
						UpdateUIMode(mIsUIModeInput);
					}
			});
			
			mTitleView = (TextView)titleView.findViewById(R.id.alertTitle);
			if (mTitleView != null) mTitleView.setText(title);
		}
		builder.setCustomTitle(titleView);
		
		// add custom view
		View contentView = getActivity().getLayoutInflater().inflate(R.layout.layout_setting_net_config_accept_point, null);
		mAdapter = new SelectorItemAdapter(getActivity());
		mListView = (ListView) contentView.findViewById(R.id.listViewAcceptPoint);
		mListView.setAdapter(mAdapter);
		
		mEditText = (EditText) contentView.findViewById(R.id.editTextSelectTemplate);
		if (mEditText != null && mLastEditInputDataString != null) 
			mEditText.setText(mLastEditInputDataString);
		

		UpdateUIMode(mIsUIModeInput);
    	builder.setView(contentView);
		

		CustomAlertDialog dialog = (CustomAlertDialog)builder.create();
		dialog.SetSuperDelegated(new CustomAlertDialog.OnSuperDelegated() {
			@Override
			public void onCreateComplete(Dialog dialog) {
		        ((CustomAlertDialog)dialog).GetAlertControl().GetButtonPanel().getChildAt(0).setVisibility(View.GONE);
			}
		});
		
		return dialog;
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mOnListener = (onCustomDialogWithAcceptPointRefreshListener)activity;
		} catch (ClassCastException e) {
			//dismiss();
		}
	}
	
	private class SelectorItemAdapter extends BaseAdapter implements OnClickListener, OnCheckedChangeListener {
		private Context mContext;
		private LayoutInflater mLayoutInflater;
		
		public SelectorItemAdapter(Context context) {
			mContext = context;
			mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		@Override
		public int getCount() {
			return mItemInfoLinkedList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = mLayoutInflater.inflate(R.layout.layout_setting_net_config_accept_point_item, null);
			
			convertView.setTag(position);
			convertView.setOnClickListener(this);
			
			TextView title = (TextView) convertView.findViewById(R.id.textViewMainTitle);
			if (title != null) title.setText(mItemInfoLinkedList.get(position));
			
			CheckBox isSelectedBox = (CheckBox) convertView.findViewById(R.id.checkBoxSelected);
			if (isSelectedBox != null) {
				isSelectedBox.setOnCheckedChangeListener(this);
				isSelectedBox.setTag(position);
				isSelectedBox.setChecked(mSelectIndex == position ? true : false);
				if (position == 0) isSelectedBox.setVisibility(View.GONE);
			}
			
			return convertView;
		}

		@Override
		public void onClick(View view) {
			int position = ((Integer) view.getTag()).intValue();
			if (position == 0) {
				if (mOnListener != null) mOnListener.onCustomDialogWithAcceptPointRefreshPleaseListener();
			} else {
				if (mOnListener != null) mOnListener.onCustomDialogWithAcceptPointSelectListener(mUniqueIdentifier, position, mItemInfoLinkedList);							
			}
			((CustomAlertDialog)getDialog()).GetAlertControl().mDialogInterface.dismiss();
		}

		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
			if (arg1) {
				int position = ((Integer) arg0.getTag()).intValue();
				if (position == 0) {
					if (mOnListener != null) mOnListener.onCustomDialogWithAcceptPointRefreshPleaseListener();
				} else {
					if (mOnListener != null) mOnListener.onCustomDialogWithAcceptPointSelectListener(mUniqueIdentifier, position, mItemInfoLinkedList);							
				}
				((CustomAlertDialog)getDialog()).GetAlertControl().mDialogInterface.dismiss();
			}
		}
	}
}
